#import "XMLoginManager.h"
#import "RCIM.h"
#import "AppDelegate.h"


@implementation XMLoginManager
+ (XMLoginManager *)sharedInstance {
    static XMLoginManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
- (void)doLoginResult:(XMUserModel *)userModel {
    [[XMLoginManager sharedInstance] saveLoginData:userModel
                                            complete:^{
                                                [[AppDelegate sharedAppDelegate] configAndEnterHome];
                                            }];
}
#pragma mark - 保存用户信息
- (void)saveLoginData:(XMUserModel *)userModel complete:(void (^)(void))complete {
    if (![XMUtil isBlankString:userModel.token]
        && ![XMUtil isBlankString:userModel.rongtoken]) {
        [kMMKV setString:userModel.token forKey:kLoginToken];
        [kMMKV setString:userModel.rongtoken forKey:kRCToken];
        [kMMKV setString:userModel.user_id forKey:kUserId];
        [kMMKV setString:userModel.nickname forKey:kUserNickName];
        [kMMKV setString:userModel.avatar.file_url forKey:kUserHeader];
        RCUserInfo *currentUserInfo = [[RCUserInfo alloc] initWithUserId:userModel.user_id name:userModel.nickname portrait:userModel.avatar.file_url];
        [RCIM sharedRCIM].currentUserInfo = currentUserInfo;
        if (complete) {
            complete();
        }
    }
}
- (void)removeLoginData {
    [kMMKV removeValueForKey:kLoginToken];
    [kMMKV removeValueForKey:kRCToken];
    [kMMKV removeValueForKey:kUserId];
    [kMMKV removeValueForKey:kUserNickName];
    [kMMKV removeValueForKey:kUserHeader];
}
- (NSString *)token {
    
    NSLog(@"%@", [kMMKV getStringForKey:kLoginToken defaultValue:@""]);
    return [kMMKV getStringForKey:kLoginToken defaultValue:@""];
}
- (NSString *)rongToken {
    return [kMMKV getStringForKey:kRCToken defaultValue:@""];
}
- (NSString *)userId {
    return [kMMKV getStringForKey:kUserId defaultValue:@""];
}
- (NSString *)nickName {
    return [kMMKV getStringForKey:kUserNickName defaultValue:@""];
}
- (NSString *)avatar {
    return [kMMKV getStringForKey:kUserHeader defaultValue:@""];
}
- (BOOL)isLogin {
    if (![NSString isEmpty:self.token] && ![NSString isEmpty:self.rongToken]) {
        return YES;
    }
    return NO;
}
@end
